#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    string alienOrder(vector<string>& words) {
        int m = words.size();
        unordered_map<char, unordered_set<char>>hashi;
        // unordered_set<char>mp;
        unordered_map<char, int>in;
        for (auto& s : words)
        {
            for (auto e : s)
            {
                in[e] = 0;
            }
        }


        for (int i = 0; i < m; i++)
        {
            for (int j = i + 1; j < m; j++)
            {
                int left = 0, right = 0;
                int M = words[i].size(), N = words[j].size();

                while (left < M && right < N)
                {
                    if (words[i][left] != words[j][right])
                    {
                        char a = words[i][left], b = words[j][right];
                        if (!hashi.count(a) || !hashi[a].count(b))
                        {
                            hashi[a].insert(b);
                            in[b]++;
                        }
                        break;

                    }
                    left++, right++;

                }
                if (right == N && left < M) return "";
            }
        }
        queue<char>q;
        string str;
        for (auto& [a, b] : in)
        {
            if (b == 0) q.push(a);
        }
        while (q.size())
        {
            char ch = q.front();
            str += ch;
            q.pop();
            for (auto e : hashi[ch])
            {
                if (--in[e] == 0) q.push(e);
            }
        }
        for (auto& [a, b] : in)
        {
            if (b) return "";
        }
        return str;
    }
};